package com.lucenly.card.view.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lucenly.card.R;
import com.lucenly.card.constants.UrlDefend;
import com.lucenly.card.event.StringMsgEvent;
import com.vise.xsnow.event.BusManager;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;
import com.vise.xsnow.utls.SharedPreferencesUtil;

public class ZfbDialog extends Dialog{
    Activity context;
    TextView tv_sure;
    public ZfbDialog(@NonNull Activity context) {
        super(context, R.style.alert_dialog);
        this.context = context;
        setContentView(R.layout.dialog_zfbi);
        tv_sure = findViewById(R.id.tv_sure);
        final EditText et_name = findViewById(R.id.et_name);
        final EditText et_number = findViewById(R.id.et_number);
        String zfb_name = SharedPreferencesUtil.getInstance().getString("zfb_name", "");
        String zfb_number = SharedPreferencesUtil.getInstance().getString("zfb_number", "");
        et_name.setText(zfb_name);
        et_number.setText(zfb_number);
        tv_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tv_sure.setEnabled(false);
                SharedPreferencesUtil.getInstance().putString("zfb_name", et_name.getText().toString().trim());
                SharedPreferencesUtil.getInstance().putString("zfb_number", et_number.getText().toString().trim());
                tixian(et_name.getText().toString().trim(),et_number.getText().toString().trim());
            }
        });
        initView(context);
    }

    /**
     * 申请提现
     * @param trim
     * @param trim1
     */
    private void tixian(String trim, String trim1) {
        ViseHttp.POST(UrlDefend.TIXIAN)
                .addHeader("Token", SharedPreferencesUtil.getInstance().getString("Token"))
                .addParam("names",trim)
                .addParam("account",trim1)
                .request(new ACallback<String>() {
                    @Override
                    public void onSuccess(String data) {
                        tv_sure.setEnabled(true);
                        BusManager.getBus().post(new StringMsgEvent("TIXIAN"));
                        TixianDialog tixianDialog = new TixianDialog(context,0);
                        tixianDialog.show();
                        dismiss();

                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        tv_sure.setEnabled(true);
                        Toast.makeText(context, errMsg, Toast.LENGTH_SHORT).show();
                        TixianDialog tixianDialog = new TixianDialog(context,1);
                        tixianDialog.show();
                        dismiss();
                    }
                });
    }

    private void initView(Context context) {
        LinearLayout lLayout_bg = (LinearLayout) findViewById(R.id.lLayout_bg);
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        setCanceledOnTouchOutside(false);
        // 调整dialog背景大小
        lLayout_bg.setLayoutParams(new FrameLayout.LayoutParams((int) (display
                .getWidth() * 0.8), LinearLayout.LayoutParams.WRAP_CONTENT));
        setCanceledOnTouchOutside(true);
    }
}
